package com.lucweb.dao.impl;

import com.lucweb.dao.GzDao;
import com.lucweb.entity.TaskGz;
import org.hibernate.SessionFactory;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;



@Transactional(rollbackFor = Exception.class)
@Repository("gzDao")
public class GzDaoImpl implements GzDao {

    @Resource(name="sessionFactory")
    private SessionFactory sessionFactory;


    @Override
    public List<TaskGz> GzList(String para) {
        List<TaskGz> gzList = (List<TaskGz>) sessionFactory.getCurrentSession().createQuery("FROM TaskGz where companyName ='"+para+"'").list();

        return gzList;
    }

    @Override
    public void saveOrUpdate(TaskGz taskGz) {
        sessionFactory.getCurrentSession().saveOrUpdate(taskGz);

    }

    @Override
    public TaskGz getGzById(int taskId) {
        return (TaskGz) sessionFactory.getCurrentSession().get(TaskGz.class, taskId);
    }

    @Override
    public void deleteGz(int taskId) {

        sessionFactory.getCurrentSession().delete(
                sessionFactory.getCurrentSession().get(TaskGz.class, taskId)
        );
    }

    @Override
    public List<TaskGz> showGz() {
        List<TaskGz> tellList = (List<TaskGz>) sessionFactory.getCurrentSession().createQuery("FROM TaskGz").list();

        return tellList;
    }

    @Override
    public int getmaxId() {
        Integer number = (Integer) sessionFactory.getCurrentSession().createSQLQuery("select auto_increment from " +
                "information_schema.tables where table_schema='firesystem' and table_name='task_gz';")
                .addScalar("auto_increment", StandardBasicTypes.INTEGER).uniqueResult();
        return number;
    }
}
